$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        detailGird:null,
        recordGrid:null,
        staffNumber:null,
        init:function () {
            this.staffNumber = xhTool.getQuery('staffNumber');
            this.detailGird = new xhDataGrid($('#detailList'),'hrm/attendance/getDetail',
                {
                    staffNumber:this.staffNumber,
                    startTime:xhTool.getQuery('startTime'),
                    endTime:xhTool.getQuery('endTime')
                });
            this.detailGird.parseToolbar().init();
            this.recordGrid = new xhDataGrid($('#recordList'),'hrm/attendance/getRecord',{staffNumber:this.staffNumber});
            this.recordGrid.parseToolbar().init().activeButton(['bt_list_fixSign']);
            this.listener();
            this.funObj().bindDgEvent();
        },
        listener:function () {
            var $this = this;

            $('#bt_list_fixSign').click(function () {
                if (xhExtra.buttonDisabled($(this))) {
                    return false;
                }
                $this.funObj().changeSign();
            });

            $('#bt_list_search').click(function () {
                $this.funObj().doSearch();
            });
            xhTool.bindDocKeyDown($('#bt_list_search'));
        },
        funObj:function () {
            var $this = this;
            return {
                bindDgEvent:function () {
                    $this.detailGird.onLoadSuccess = function (rowData) {
                        var total = rowData['total'];
                        var rows = rowData['rows'];
                        var currentDay = xhTool.getQuery('currentDay');
                        if (currentDay) {
                            $.each(rows ,function (index,item) {
                                if (item['sDay'] == currentDay) {
                                    $this.detailGird.command('selectRow', index);
                                }
                            })
                        } else {
                            $this.detailGird.command('selectRow',0);
                        }
                    };
                    $this.detailGird.command({
                        rowStyler: function(index,row){
                            if (row.bHoliday==1){
                                return 'background-color:#CCFFCC;'; // return inline style
                            }
                        }
                    })
                    $this.detailGird.onSelect = function (rowIndex, rowData) {
                        $this.recordGrid.reload({
                            staffNumber:this.staffNumber,
                            currentDay:rowData['sDay']
                        });
                        $this.recordGrid.command('getPanel').panel('setTitle', rowData['sStaffName']+'的打卡记录（' + rowData['sDay'] + '）');
                    }
                },
                changeSign:function () {
                    var sDay = $this.recordGrid.getSelectValue('sDay');
                    var sSignTime = $this.recordGrid.getSelectValue('sSignTime');
                    window.top.swal({
                        title: '修改打卡记录',
                        confirmButtonText: '增加',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        html: '<input id="day" class="swal2-input" placeholder="日期必填" value="'+sDay+'" />' +
                        '<select id="signType" class="swal2-select" style="width: 100%;">' +
                            '<option value="1" selected>上班打卡</option>' +
                            '<option value="2">下班打卡</option>' +
                        '</select>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var day   = $('#day', window.top.document).val();
                                if (!day){
                                    reject('未填写日期！');
                                } else {
                                    resolve(day);
                                }
                            })
                        }
                    }).then(function (day) {
                        var signType   = $('#signType', window.top.document).val();
                        xhExtra.ajaxRequest('',{day:day,signType:signType,signTime:sSignTime},{},function (res) {
                            window.top.xhTop.showSuccess(res.msg);
                            $this.detailGird.reload();
                        })
                    },function (dismiss) {
                        return false;
                    });
                },
                doSearch:function () {
                    $this.detailGird.reload({
                        key: xhExtra.getValue($('#list_search')),
                        startTime:xhExtra.getValue($('#List_Search_StartTime')),
                        endTime:xhExtra.getValue($('#List_Search_EndTime')),
                        staffNumber:$this.staffNumber
                    });
                }
            }
        }
    }
};